package com.kbt.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.*;
import com.google.gson.Gson;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.model.data.BaseBean;
import com.kbt.net.RequestManager;
import com.kbt.util.tool.BtnSetting;
import com.kbt.util.utils.Utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: KBT_ANDRIOD
 * @Copyright: KJ Co., Ltd. Copyright 2015-2020,  All rights reserved
 * @Description: ${todo}帮助与反馈
 * @author: zhgk
 * @data: 2015/8/18 17:12
 * @version: V1.0
 */
public class HelpAndFeedBackActivity extends Activity implements RequestManager.ResponseInterface{

    private int currentBtn = 0;

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private List<Button> btnList = new ArrayList<Button>();

    private Button btnSub;

    private EditText feedbackEditText;

    private EditText concatMethod;
    private RequestManager mRequestManager;
    private Map<String, String> map =null;
    private BaseBean  baseBean;
    private Map<Integer,String> btnMap;
    private String btnType="1";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // setContentView(R.layout.help_add_feedback);
        this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.help_add_feedback);
        this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_return_btn);

        //设置返回按钮事件,关闭当前Activity，返回数据
        ImageView returnImg = (ImageView) findViewById(R.id.return_btn);
        returnImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //不创建地址
                setResult(RESULT_CANCELED, null);
                BaseApplication.getInstance().removeActivity(HelpAndFeedBackActivity.this);
                finish();
            }
        });

        TextView appTitleView = (TextView) findViewById(R.id.center_title);
        appTitleView.setText("问题与反馈");
    }

    @Override
    protected void onStart() {
        super.onStart();
        BaseApplication.getInstance().addActivity(HelpAndFeedBackActivity.this);
        //初始化页面
        intView();

    }

    /***
     * 初台化View
     */
    private void intView() {

        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, String>();
        btnMap = new HashMap<Integer,String>();

        btn1 = (Button) findViewById(R.id.help_btn1);
        btn1.setOnClickListener(new BtnOnClickListener());
        btnList.add(btn1);
        currentBtn = btn1.getId();
        btn2 = (Button) findViewById(R.id.help_btn2);
        btn2.setOnClickListener(new BtnOnClickListener());
        btnList.add(btn2);
        btn3 = (Button) findViewById(R.id.help_btn3);
        btn3.setOnClickListener(new BtnOnClickListener());
        btnList.add(btn3);
        btn4 = (Button) findViewById(R.id.help_btn4);
        btn4.setOnClickListener(new BtnOnClickListener());
        btnList.add(btn4);
        btn5 = (Button) findViewById(R.id.help_btn5);
        btn5.setOnClickListener(new BtnOnClickListener());
        btnList.add(btn5);
        btn6 = (Button) findViewById(R.id.help_btn6);
        btn6.setOnClickListener(new BtnOnClickListener());
        btnList.add(btn6);

        btnMap.put(btn1.getId(),"1");
        btnMap.put(btn2.getId(),"2");
        btnMap.put(btn3.getId(),"3");
        btnMap.put(btn4.getId(),"4");
        btnMap.put(btn5.getId(),"5");
        btnMap.put(btn6.getId(),"6");

        initBtnStyle(btn1.getId());

        feedbackEditText = (EditText) findViewById(R.id.feedbackEditText);
        concatMethod = (EditText) findViewById(R.id.concatAddress);


        btnSub = (Button) findViewById(R.id.help_subBtn);
        btnSub.setTextColor(Color.WHITE);
        btnSub.setOnClickListener(new SubBtnOnClickListener());

    }

    private void initBtnStyle(int btnId) {
        int size = btnList.size();
        Button temp;
        for (int i = 0; i < size; i++) {
            temp = btnList.get(i);
            if (temp.getId() == btnId) {
                btnType = btnMap.get(btnId);
                temp.setBackgroundColor(Color.RED);
                temp.setTextColor(Color.WHITE);
            } else {
                temp.setBackgroundColor(Color.WHITE);
                temp.setTextColor(Color.rgb(51, 51, 51));
            }
        }

    }

    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        if (tag == Constants.RequestTag.saveYJag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                baseBean = (BaseBean) parsedGSON;
                if(baseBean.getResult().equals("success")){
                    Toast.makeText(HelpAndFeedBackActivity.this,""+baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                    BaseApplication.getInstance().removeActivity(HelpAndFeedBackActivity.this);
                    finish();
                }else{
                    Toast.makeText(HelpAndFeedBackActivity.this,""+baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {

    }

    /****
     * 问题分类按钮选择 点击监听事件类
     */
    private class BtnOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            currentBtn = v.getId();
            initBtnStyle(v.getId());
        }
    }

    /****
     * 提交按钮，点击事件
     */
    private class SubBtnOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            String fedStr = feedbackEditText.getText().toString().trim();
            if (fedStr.length() == 0) {
                Toast.makeText(HelpAndFeedBackActivity.this, "请填写反馈意见！", Toast.LENGTH_SHORT).show();
                return;
            }
            //保存反馈意见
            httpSaveYJ();
        }
    }
    //保存反馈意见
    private void httpSaveYJ(){
        if(Utils.isNetworkAvailable(HelpAndFeedBackActivity.this)){
        map.clear();
        map.put("methodType", "submit");
        Gson gson = new Gson();
        Map dataMap = new HashMap<String,String>();
        dataMap.put("advise_type",btnType);
        dataMap.put("advise_desc", feedbackEditText.getText().toString());
        dataMap.put("advise_account",concatMethod.getText().toString());
        map.put("data", gson.toJson(dataMap));
        mRequestManager.post(Constants.ServiceInterFace.saveYJURL, map, Constants.RequestTag.saveYJag, BaseBean.class);
        }else{
            Toast.makeText(HelpAndFeedBackActivity.this,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }
}
